﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BigPang.Render;

namespace BigPang
{
    public partial class GameCanvas : UserControl
    {

        public event EventHandler<EventArgs> OnDrawFrame;

        Canvas mCanvas;

        int mFpsCount;
        int mFpsTime;
        int mFPS;

        int mLastFrameTime;

        public GameCanvas()
        {
            InitializeComponent();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (DesignMode)
            {
                base.OnPaintBackground(e);
                return;
            }

            DrawFrame();

            mCanvas.Refresh( e.Graphics );
        }

        private void DrawFrame()
        {
            mCanvas.Clear();

            if (OnDrawFrame != null)
                OnDrawFrame(mCanvas, null);

            if (Environment.TickCount - mFpsTime >= 1000)
            {
                mFPS = mFpsCount;
                mFpsCount = 0;
                mFpsTime = Environment.TickCount;
            }
            else
            {
                mFpsCount++;
            }
        }

        private void GameCanvas_Load(object sender, EventArgs e)
        {
            mCanvas = new Canvas(this.Handle);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Environment.TickCount - mLastFrameTime >= 30)
            {
                this.Invalidate();
                mLastFrameTime = Environment.TickCount;
            }
        }
    }
}
